﻿var savegeometryurl = "http://10.17.0.140:8090/iserver/services/data-CKQFBT/rest/data/datasources/name/CKQDJK/datasets/name/CKQ_CKSQDJ/";
http: //10.17.0.140:8090/iserver/services/map-CKQFBT/rest/maps/CKQFBT
var m_project_id = null;
{
    var r = new RegExp("(^|(.*?\\/))(spcommon.js)(\\?|$)"),
	s = document.getElementsByTagName('script'),
	src, m, baseurl = "";
    for (var i = 0, len = s.length; i < len; i++) {
        src = s[i].getAttribute('src');
        if (src) {
            var m = src.match(r);
            if (m) {
                baseurl = m[1];
                break;
            }
        }
    }
    inputScript(baseurl + 'SaveGeometry.js');
}
function inputScript(inc) {
    var script = '<' + 'script type="text/javascript" src="' + inc + '"' + '><' + '/script>';
    document.writeln(script);
}
//初始化对象
var m_cksqdj = new SmObjectData("CKQDJK", "采矿申请登记");
var m_metaconst = new SmConstClsList();

function loadData() {

}
$(document).ready(function () {




    //获取流程号PROJECT_ID	
    m_project_id = $.query.get("Project_ID");
    var m_hdlx = $.query.get("HDLX");
    if (m_hdlx == "") { m_hdlx = $.query.get("hdlx"); };
    //获取一条记录
    m_cksqdj.GetFirstSmObject("PROJECT_ID=\\\'" + m_project_id + "\\\'");
    //给申请页面赋值
    $(":text").each(function () {
        var ff = $(this).attr("name");
        $(this).val(m_cksqdj.GetValue(ff));
    });
    $("#txtKZMC").val(GetKZMCbyKZDM(m_cksqdj.GetValue("开采主矿种")));
    $("#txt经济类型").val(GetJJLXMCbyJJLXDM(m_cksqdj.GetValue("经济类型")));
    $("textarea").each(function () {
        var dd = $(this).attr("name");
        $(this).val(m_cksqdj.GetValue(dd));
    });
    $("#btnSCRYJ").click(function () {
        $("#txtQFYJ").val("同意【申请人】关于【矿山名称】的采矿权新立申请。");
    });
    //所有按钮不可用，根据类型显示不同按钮
    $(":button").each(function () {
        $(this).css("display", "none");
    });
    switch (m_hdlx) {
        case "JSPH":
            $("#btnSQPH").css("display", "inline");
            $("#btnJSPH").css("display", "inline");
            $("#btnPrintSPB").removeClass("nosee");
            if ($("#txtZHENGHAO").val() != "") {
                //$("#btnPrintXKZH").css("display", "inline");
                //$("#btnPrintXKZ").css("display", "inline");
            }
            break;
        case "FFXKZ":
            $("#btnPrintSPB").css("display", "inline");
            break;
        case "BLWC":
            $("#btnPrintXKZH").css("display", "inline");
            $("#btnPrintXKZ").css("display", "inline");
            if (m_cksqdj.GetValue("项目类型") == "10") {
                $("#btnBLWC").css("display", "inline");
            }
            break;
    };
    var left = $("#btnJSPH").offset().left;
    $("#id_button_jsph").css("left", left);
    if (m_hdlx != "JSPH") {
        $("#id_button_jsph").remove();
    }
   
    //alert($("#btnJSPH")[0].left);
    //申请配号
    $("#btnSQPH").click(function () {
        SQPH();
    });
    //    $("#btnPrintSPB").click(function () {
    //        window.open("../../SP_COMMON/PrintPage/CKQXL_SPZRB.shtml?PROJECT_ID=" + m_project_id + "&TITLE=采矿权申请审批责任表");
    //    }
    //	);
    $("#btnPrintXKZH").click(function () {
        window.open("../../SP_COMMON/PrintPage/CKQXL_XKZFB.shtml?PROJECT_ID=" + m_project_id);
    });
    $("#btnPrintXKZ").click(function () {
        window.open("../../SP_COMMON/PrintPage/CKQXL_XKZ.shtml?PROJECT_ID=" + m_project_id);
    });
    //打印审批表
    $("#btnPrintSPB").click(function () {
        window.open("../../SP_COMMON/PrintPage/CKQXL_SPZRB.shtml?PROJECT_ID=" + m_project_id + "&TITLE=采矿权申请审批责任表");
    });
    function SQPH() {
        $.ajax({
            async: false,
            url: DBCenterServiceUrl + "/KZ_SPDJ/SPCommon.asmx/WriteBPXML2",
            type: "POST",
            dataType: "json",
            data: "{'project_id':'" + m_project_id + "','fileName':'CKXL" + $("#txt矿山名称").val() + "'}",
            contentType: "application/json; charset=utf-8",
            success: function (json) {
                SaveAs();
            },
            error: function (x, e) {
            },
            complete: function (x) {
            }
        });
    };
    function SaveAs() {
        //        var m_SQPH = WFServiceUrl + "/temp/申请配号" + m_project_id + ".ZIP";
        var ksmc = $("#txt矿山名称").val();
        var m_SQPH = DBCenterServiceUrl + "/KZ_SPDJ/SPCommon.asmx/Download?filename=CKXL" + encodeURI(ksmc) + ".xml"

        window.location.href = m_SQPH;

    };
    //    //接收配号
    //    $("#btnJSPH").click(function () {
    //        JSPH();
    //    });
    //办理完成
    $("#btnBLWC").click(function () {
        m_cksqdj.GetFirstSmObject("PROJECT_ID=\\\'" + m_project_id + "\\\'");
        if (m_cksqdj.GetValue("SFDZ") == "0") {
            alertex("入库失败，请先打印许可证");
            return;
        }
        if (m_cksqdj.GetValue("许可证号") == null || m_cksqdj.GetValue("许可证号") == "") {
            alertex("还未进行接收配号，不能办理完成!", true);
            return;
        }
        if (m_cksqdj.GetValue("项目类型") == "1010") {
            alertex("该项目已经办理完成!", true);
            return;
        }
        m_cksqdj.SetValue("项目类型", "10");
        m_cksqdj.SetValue("XMZT", "0");
        var res = m_cksqdj.Save(DBCenterServiceUrl + "/KZ_SPDJ/KZ_SPDJ_CKSQDJ.asmx/SaveCKQXL", "'LX':'RKGD'");
        if (res == true) {
            alertex("办理完成！", true);
            $(this).css("display", "none");
            var value = m_cksqdj.GetValue("区域坐标");
            if (value != null && value != "") {

                var values = value.split(',');
                var points = GetZBC(values);
               
                var fields = ["CK_GUID", "许可证号", "开采主矿种", "矿山名称", "有效期起", "有效期止"];
                var values = [m_cksqdj.GetValue("CK_GUID"), m_cksqdj.GetValue("许可证号"), m_cksqdj.GetValue("开采主矿种"), m_cksqdj.GetValue("矿山名称"), GetKZMCbyKZDM(m_cksqdj.GetValue("有效期起")), GetKZMCbyKZDM(m_cksqdj.GetValue("有效期止"))];
                saveGeometry(points, savegeometryurl, fields, values);
            }
            alert("提交入库成功!");
        }
        else alertex("办理完成失败！", true);
    });
    $(":text").attr("readonly", "readonly");
    $("textarea").attr("readonly", "readonly");

});

function JSPH() {
    var ss = document.getElementById("id_input_file");
    ss.click();
}

function getPath(obj) {
    if (obj) {
        if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
            obj.select();
            return document.selection.createRange().text;
        }
        else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            if (obj.files) {
                return obj.files.item(0).getAsDataURL();
            }
            return obj.value;
        }
        return obj.value;
    }
}
function funfilechange() {
    $("form:first").submit();
}
function selectedFileChanged() {
    var fileselector = document.getElementById("id_input_file");
    $("#id_input_selectedfile").val(fileselector.value);
    var path = getPath(fileselector);
    var appname, appcategory, apptype, ckguid, licenceid;
    if ($.browser.msie) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = true;
        xmlDoc.load(path);
        appname = xmlDoc.getElementsByTagName("app_name")[0].childNodes[0].nodeValue;
        appcategory = xmlDoc.getElementsByTagName("app_category")[0].childNodes[0].nodeValue;
        apptype = xmlDoc.getElementsByTagName("app_type")[0].childNodes[0].nodeValue;
        ckguid = xmlDoc.getElementsByTagName("ck_guid")[0].childNodes[0].nodeValue;
        licenceid = xmlDoc.getElementsByTagName("licence_id")[0].childNodes[0].nodeValue;
        validate(appname, appcategory, apptype, ckguid, licenceid);
    }
    else if ($.browser.safari) {
        var xmlDom = document.implementation.createDocument("", "doc", null);
        var filereader = new FileReader();
        filereader.readAsText(fileselector.files[0]);
        filereader.onloadend = function () {
            var str = this.result;
            var jsonobj = $.xml2json(str);
            appname = jsonobj.app_name;
            appcategory = jsonobj.app_category;
            apptype = jsonobj.app_type;
            ckguid = jsonobj.ck_guid;
            licenceid = jsonobj.licence_id;
            validate(appname, appcategory, apptype, ckguid, licenceid);
        }
    }
    fileselector.value = '';
}

function validate(appname, appcategory, apptype, ckguid, licenceid) {
    if (appname != m_cksqdj.GetValue("矿山名称") || appcategory != "采矿权" || apptype != "采矿权新立") {
        alertex("导入的配号文件与当前项目不符", true);
        return;
    }
    $("#txtZHENGHAO").val(licenceid);
    m_cksqdj.SetValue("许可证号", licenceid);
    m_cksqdj.Update();
    alertex("读取配号成功", true);
    //$("#btnPrintXKZH").css("display", "inline");
    //$("#btnPrintXKZ").css("display", "inline");
}

function afterJSPH(result) {
    if (result == "1") {

        location.reload();
    }
    else {
        alert(result);
    }
}

var m_project_id = '';
var m_xkzh = '';
var m_obj = null;
var sfmap = null;

{
    var r = new RegExp("(^|(.*?\\/))(spcommon.js)(\\?|$)"),
	s = document.getElementsByTagName('script'),
	src, m, baseurl = "";
    for (var i = 0, len = s.length; i < len; i++) {
        src = s[i].getAttribute('src');
        if (src) {
            var m = src.match(r);
            if (m) {
                baseurl = m[1];
                break;
            }
        }
    }
    inputScript(baseurl + 'SaveGeometry.js');
}
function inputScript(inc) {
    var script = '<' + 'script type="text/javascript" src="' + inc + '"' + '><' + '/script>';
    document.writeln(script);
}